<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\User;
use App\Models\UserContact;
use App\Supports\Sms\Sms;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use App\Services\UpdateUserInfo\UpdateUserInfo;
use App\Services\RegisterNewUser\RegisterNewUser;
use App\Services\SearchTeamMember\SearchTeamMember;
use App\Services\UpdateUserInfo\UpdateUserInfoRequest;
use App\Services\AttendUser\AttendUserRequest;
use App\Services\AttendUser\AttendUser;
use App\Services\UserFromInviteCode\UserFromInviteCode;
use App\Http\Controllers\Api\V1\Resources\UserResource;
use App\Services\RegisterNewUser\RegisterNewUserRequest;
use App\Services\SearchTeamMember\SearchTeamMemberRequest;
use App\Http\Controllers\Api\V1\Requests\UserUpdateRequest;
use App\Services\UserFromInviteCode\UserFromInviteCodeRequest;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class UsersController extends Controller
{
	public function store()
	{

		$lock = Cache::lock('register_new_user', 15);

		try {

			if (! $lock->get()) return fail('请重试');

			$request = app(RegisterNewUserRequest::class);


			app(RegisterNewUser::class, [
				'name' => $request->name,
				'phone' => $request->phone,
				'password' => $request->password,
				'inviteCode' => $request->invite_code
			])->register();

			app(Sms::class)->cleanCachedDataAfterUsing(request('sms_key'));

            return response(null, 204);

		} finally {
			$lock->release();
		}
	}

    public function show()
    {
	    /**
	     * @var User $user
	     */
    	$user = request()->user();

    	$user = User::query()->where("id",$user->id)->where("grade","vip")->first();


    	return $user ?  new UserResource($user) : [];
    }

     public function showAll()
    {
	    /**
	     * @var User $user
	     */
    	$user = request()->user();
    	
    	
    	return  new UserResource($user);
    }

    public function fromInviteCode(UserFromInviteCodeRequest $request): UserResource
    {
    	$user = app(UserFromInviteCode::class, ['inviteCode' => $request->invite_code])->get();
	    return new UserResource($user);
    }

    public function team(): AnonymousResourceCollection
    {
	    /**
	     * @var User $user
	     */
	    $user = request()->user();
	    return UserResource::collection($user->myTeamMembers());
    }

    public function search(SearchTeamMemberRequest $request): AnonymousResourceCollection
    {
    	$members = app(SearchTeamMember::class, [
    		'user' => $request->user(),
    		'scope' => $request->scope,
		    'keyword' => $request->keyword
	    ])->search();

	    return UserResource::collection($members);
    }

    public function todayBonusRank(): JsonResponse
    {
	    /**
	     * @var User $user
	     */
    	$user = request()->user();
    	$index = (int)request('index', 0);
	    return response()->json($user->dayBonusRank($index));
    }

    public function allBonusRank(): JsonResponse
    {
	    /**
	     * @var User $user
	     */
	    $user = request()->user();
	    $index = (int)request('index', 0);
	    return response()->json($user->allBonusRank($index));
    }

    public function update()
    {
	    $lock = Cache::lock('update_user_info', 15);

	    try {

	    	if (! $lock->get()) fail('请重试');

	    	$request = app(UpdateUserInfoRequest::class);

		    app(UpdateUserInfo::class, [
			    'request' => $request,
		    ])->update();

            return response(null, 204);

	    } finally {
		    $lock->release();
	    }
    }


     public function attend()
    {
	    $lock = Cache::lock('attend_user', 1);

	    try {

	    	if (! $lock->get()) fail('请重试');

	    	$request = app(AttendUserRequest::class);

		    app(AttendUser::class, [
			    'request' => $request,
		    ])->attend();

            return response(null, 204);

	    } finally {
		    $lock->release();
	    }
    }

    public function setChildNickname(User $user)
    {
        /**
         * @var User $user
         */
        $user = request()->user();
    	$user->setChildNickname($user);
	    return response(null, 204);
    }

	public function support()
	{
		$lock = Cache::lock('beans_support', 15);

		try {

			if (! $lock->get()) fail('处理中...请勿频繁操作');

			$request = app(GoldStormSupportRequest::class);

			/**
			 * @var User $user
			 */
			$user = $request->user();

			need_paycode($user);

			app(GoldStormSupport::class, [
                'child' => $request->getChild()
            ])->support();

			$user->resetPayCodeTry();

            return response(null, 204);

		} finally {
			$lock->release();
		}
	}

	public function contact()
	{
		$lock = Cache::lock('user_contact', 15);

		$productId = request()->product_id;
		if(!$productId){
			fail('参数错误');
		}
		try {

			if (! $lock->get()) fail('处理中...请勿频繁操作');


			/**
			 * @var User $user
			 */
			$user = request()->user();

			$insertArr = [
				'product_id' => $productId,
				'user_id'=>$user->id,
			];
			
			return UserContact::create($insertArr);

		} finally {
			$lock->release();
		}
	}
}
